package org.example.printexercise;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class PracticeActivity extends AppCompatActivity {
    private LinearLayout contentContainer;
    private List<String> lines;
    private List<EditText> inputFields;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);

        contentContainer = findViewById(R.id.contentContainer);
        inputFields = new ArrayList<>();

        // 示例文本，实际应用中可以从其他地方获取
        String sampleText = "This is the first line of text to test the display effect of longer sentences.\nThis is the second line of text.\nThis is the third line of text to demonstrate the effect of automatic line wrapping.\nThis is the fourth line of text.\nThis is the fifth line of text to test the display effect of texts with different lengths.";
        lines = new ArrayList<>();

        // 按行分割文本
        String[] splitLines = sampleText.split("\n");
        for (String line : splitLines) {
            lines.add(line);
        }

        setupTypingInterface();
    }

    private void setupTypingInterface() {
        for (int i = 0; i < lines.size(); i++) {
            // 添加原文本行
            TextView originalText = new TextView(this);
            originalText.setText(lines.get(i));
            originalText.setTextSize(18);
            originalText.setPadding(16, 16, 16, 8);
            originalText.setTextColor(Color.BLACK); // 设置原文本为黑色
            contentContainer.addView(originalText);

            // 添加用户输入行
            EditText userInput = new EditText(this);
            userInput.setHint(lines.get(i)); // 使用hint显示浅色占位符
            userInput.setTextSize(18);
            userInput.setPadding(16, 8, 16, 16);

            // 设置hint文字颜色为浅灰色
            userInput.setHintTextColor(0xFFCCCCCC);

            // 启用自动换行
            userInput.setHorizontallyScrolling(false);
            userInput.setMaxLines(5);

            // 添加文本变化监听器实现自动跳转和错误标记
            final int currentIndex = i;
            userInput.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    String expected = lines.get(currentIndex);
                    String actual = s.toString();

                    // 检查是否输入完成并自动跳转到下一行
                    if (actual.length() >= expected.length() && currentIndex < inputFields.size() - 1) {
                        // 延迟执行，确保当前输入已完成
                        userInput.post(() -> {
                            if (currentIndex + 1 < inputFields.size()) {
                                inputFields.get(currentIndex + 1).requestFocus();
                            }
                        });
                    }

                    // 实时标记错误字符
                    Editable editable = userInput.getEditableText();
                    // 清除之前的颜色标记
                    android.text.Spannable spannable = (android.text.Spannable) editable;
                    android.text.style.CharacterStyle[] spans = spannable.getSpans(0, spannable.length(), android.text.style.CharacterStyle.class);
                    for (android.text.style.CharacterStyle span : spans) {
                        spannable.removeSpan(span);
                    }

                    // 标记错误字符
                    for (int j = 0; j < Math.min(expected.length(), actual.length()); j++) {
                        if (expected.charAt(j) != actual.charAt(j)) {
                            spannable.setSpan(
                                    new android.text.style.ForegroundColorSpan(Color.RED),
                                    j, j + 1,
                                    android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            );
                        }
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {}
            });

            contentContainer.addView(userInput);
            inputFields.add(userInput);
        }
    }
}
